package com.edu.service;

import com.edu.entity.User;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.stereotype.Service;


@Service
public class UserService {
    private final Cache<Integer, User> cache = Caffeine.newBuilder().build();

    public User getUserById(Integer key) {
        // 尝试从缓存中获取数据  
        User user = cache.getIfPresent(key);
        if (user == null) {
            // 缓存中没有数据，从数据库中获取
            System.out.println("从数据库中查询");
            user = getUserFromDatabase(key);
            // 将从数据库中获取的数据放入缓存
            cache.put(key, user);
        } else {
            System.out.println("从缓存中查询");
            return user;

        }
        return user;

    }

    public static User getUserFromDatabase(Integer key) {
        // 从数据库中查询数据的逻辑
        return new User(key, "sample userName", "sample password");
    }

}

  
        
